package com.resou.reader.bookstore.more;

import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.data.Injection;
import com.resou.reader.data.bookstore.BookStoreRepository;
import com.resou.reader.data.bookstore.model.MorePlateInfo;
import com.resou.reader.data.bookstore.response.PlateInfoResponse;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class MorePresenter extends ResouBasePresenter<MoreContract.View> implements MoreContract.Presenter {

    private final BookStoreRepository mBookStoreRepository;
    private boolean isRequestInProgress = false;
    private int mCurrentPage = 0;

    public MorePresenter(MoreContract.View view) {
        super(view);
        mBookStoreRepository = Injection.provideBookstoreRepository();
    }

    private static final String TAG = "MorePresenter";

    @Override
    public void loadData(String gender, String plateType, int pageNum) {
        mView.showProgress();
        addCompositeDisposable(
                mBookStoreRepository.getMoreInfo(gender, plateType, mCurrentPage, pageNum)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(responseResult -> {
                                    if (responseResult.getCode() == 0) {
                                        PlateInfoResponse<MorePlateInfo> resultData = responseResult.getData();
                                        List<MorePlateInfo> plateInfo = resultData.getPlateInfo();
                                        mCurrentPage = mCurrentPage + 1;
                                        mView.addData(plateInfo);
                                        mView.showContent();
                                    }
                                }, throwable -> {
                                    mView.loadFailed();
                                    mView.showError(throwable);
                                }
                        )
        );
    }

    @Override
    public void loadNextData(String gender, String plateType, int pageNum) {
        addCompositeDisposable(
                mBookStoreRepository.getMoreInfo(gender, plateType, mCurrentPage, pageNum)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(responseResult -> {
                            if (responseResult.getCode() == 0) {
                                PlateInfoResponse<MorePlateInfo> resultData = responseResult.getData();
                                List<MorePlateInfo> plateInfo = resultData.getPlateInfo();
                                mCurrentPage = mCurrentPage + 1;
                                mView.addData(plateInfo);
                            }
                        }, throwable -> {
                            mView.loadFailed();
                            mView.showError(throwable);
                        })
        );
    }

    public int getPageIndex() {
        return mCurrentPage;
    }

}
